import DataCenter from "./DataCenter";
import SerialPortManager from "./SerialPortManager"
import { ACK_Type, Adjust_Model, Control_Level, ProductID, Topic_Control, Topic_Update_LightMode } from "./LogicConstant";
import CommunicationManager from "./CommunicationManager";
import { formatterSendTopic, formatterTopic } from "./TopicUtil";
import { commonMakeMqttRequest, commonMakeGateBrightControlCmd, commonMakeGateStrategyControlCmd, commonMakeControlRequest } from "./Utils";
import XLog from "../libs/XLog";
import { DeviceEventEmitter } from "react-native";

export default class UICommandExecute {
    //本地网关实时调光
    static onLocalRealSlider(loops, value) {
        // try{
        //     //loops是回路数组
        //     console.log("slider loops:",loops,value);
        //     //发送数据到灯光
        //     // SerialPortManager.controlLight(loops, value);
        //     for(var item of loops){
        //         SerialPortManager.controlLight(item, value);
        //     }
        //     SerialPortManager.controlLightEnd();
        // }catch(error) {
        //     XLog.e("onLocalRealSlider exception:", error?.message);
        // }
    }
    //本地网关调光结束
    static onLocalSliderComplete(loops, value, needCache) {
        try{
            //loops是回路数组
            console.log("slider complete loops:",loops,value);
            if(!needCache){//非缓存都不触发消息，不再支持实时调整
                return;
            }
            //更新本地网关缓存数据
            //发送数据到灯光
            //发送控制指令到master节点，供master节点同步数据
            let control_cmd = [];
            for(var item of loops){
                SerialPortManager.controlLight(item, value);
                //构造发送指令
                control_cmd.push(commonMakeGateBrightControlCmd(item, value));
            }
            SerialPortManager.controlLightEnd();
            
            if(needCache){
                DataCenter.setColonyCabinetDeviceLight(DataCenter.gateId, loops, value);
                DataCenter.setSegmentCabinetDeviceLightByGateId(DataCenter.gateId, loops, value);
                DeviceEventEmitter.emit("localCabinet");
                CommunicationManager.send2Master(formatterSendTopic(Topic_Control),
                    commonMakeControlRequest(
                        Control_Level.LEVEL_LOOP,
                        needCache,
                        0,
                        DataCenter.gateId,
                        Adjust_Model.ADJUST_DIRECT_LIGHT,
                        "",control_cmd));
            }
        }catch(error) {
            XLog.e("onLocalSliderComplete exception:", error?.message);
        }
    }
    //本地网关修改策略
    static onLocalStrategySelect(loops, strategyId) {
        try{
            //loops是回路数组
            console.log("strategy:", loops, strategyId);
            //更新本地网关缓存数据
            //创建策略闹钟
            //发送控制指令到master节点，供master节点同步数据
            let control_cmd = [];
            for(var item of loops){
                //构造发送指令
                control_cmd.push(commonMakeGateStrategyControlCmd(item, strategyId));
            }
            DataCenter.setColonyCabinetDeviceStrategyId(DataCenter.gateId, loops, strategyId);
            DataCenter.setSegmentCabinetDeviceStrategyByGateId(DataCenter.gateId, loops, strategyId);
            DeviceEventEmitter.emit("localCabinet");
            CommunicationManager.send2Master(formatterSendTopic(Topic_Control), 
            commonMakeControlRequest(
                Control_Level.LEVEL_LOOP,
                true,
                0,
                DataCenter.gateId,
                Adjust_Model.ADJUST_STRATEGY_LIGHT,
                "",control_cmd));
        }catch(error) {
            XLog.e("onLocalStrategySelect exception:", error?.message);
        }
    }

    //集群网关实时调光
    static onColonyRealSlider(gateId, loops, value) {
        // try{
        //     console.log("colony real slider loops:",gateId, loops,value);
        //     let control_cmd = [];
        //     for(var item of loops){
        //         //构造发送指令
        //         control_cmd.push(commonMakeGateBrightControlCmd(item, value));
        //     }
        //     CommunicationManager.send2Master(formatterSendTopic(Topic_Control), 
        //     commonMakeControlRequest(
        //         Control_Level.LEVEL_LOOP,
        //         false,
        //         0,
        //         gateId,
        //         Adjust_Model.ADJUST_DIRECT_LIGHT,
        //         "",control_cmd));
        // }catch(error) {
        //     XLog.e("onColonyRealSlider exception:", error?.message);
        // }
    }

    //集群网关调光结束
    static onColonySliderComplete(gateId, loops, value, needCache) {
        try{
            //console.log("colony complete slider loops:",gate, loops, value, needCache);
            //更新本地集群数据
            //通知master
            if(!needCache){//非缓存都不触发消息，不再支持实时调整
                return;
            }
            let control_cmd = [];
            for(var item of loops){
                //构造发送指令
                control_cmd.push(commonMakeGateBrightControlCmd(item, value));
            }
            if(needCache) {
                DataCenter.setColonyCabinetDeviceLight(gateId, loops, value);
                DataCenter.setSegmentCabinetDeviceLightByGateId(gateId, loops, value);
                CommunicationManager.send2Master(formatterSendTopic(Topic_Control), 
                commonMakeControlRequest(
                    Control_Level.LEVEL_LOOP,
                    needCache,
                    0,
                    gateId,
                    Adjust_Model.ADJUST_DIRECT_LIGHT,
                    "",control_cmd));
            }
        }catch(error) {
            XLog.e("onColonySliderComplete exception:", error?.message);
        }
    }

    //集群网关策略修改
    static onColonyStrategySelect(gateId, loops, strategyId) {
        try{
            //更新本地集群数据
            //通知master
            let control_cmd = [];
            for(var item of loops){
                //构造发送指令
                control_cmd.push(commonMakeGateStrategyControlCmd(item, strategyId));
            }
            DataCenter.setColonyCabinetDeviceStrategyId(gateId, loops, strategyId);
            DataCenter.setSegmentCabinetDeviceStrategyByGateId(gateId, loops, strategyId);
            CommunicationManager.send2Master(formatterSendTopic(Topic_Control), 
                commonMakeControlRequest(
                    Control_Level.LEVEL_LOOP,
                    true,
                    0,
                    gateId,
                    Adjust_Model.ADJUST_STRATEGY_LIGHT,
                    "",control_cmd));
        }catch(error) {
            XLog.e("onColonyStrategySelect exception:", error?.message);
        }
    }

    //单洞实时调光
    static onSingleHoleRealSlider(sectionId, value) {
        // try{
        //     //判断本机是否在section段内，如果在，应该要发送指令
        //     let loops = DataCenter.queryLoopsInSection(sectionId);
        //     for(var item of loops.local){
        //         SerialPortManager.controlLight(item, value);
        //     }
        //     SerialPortManager.controlLightEnd();
        // }catch(error) {
        //     XLog.e("onSingleHoleRealSlider exception:", error?.message);
        // }
    }

    //单洞调光结束
    static onSingleHoleSliderComplete(sectionId, value, needCache) {
        try{
            if(!needCache) {
                //对于段调整，不再支持实时调整
                return;
            }
            //判断本机是否在section段内，如果在，应该要发送指令
            let loops = DataCenter.queryLoopsInSection(sectionId);
            for(var item of loops.local){
                SerialPortManager.controlLight(item, value);
            }
            SerialPortManager.controlLightEnd();

            DataCenter.setColonyCabinetDeviceLight(DataCenter.gateId, loops.local, value);
            for(let gate of Object.keys(loops.colony)) {
                DataCenter.setColonyCabinetDeviceLight(gate, loops.colony[gate], value);
            }
            DataCenter.setSegmentCabinetDeviceLight(sectionId, value);
            CommunicationManager.send2Master(formatterSendTopic(Topic_Control), 
                commonMakeControlRequest(
                    Control_Level.LEVEL_SEGMENT,
                    needCache,
                    sectionId,
                    0,
                    Adjust_Model.ADJUST_DIRECT_LIGHT,
                    value,[]));
    
        }catch(error) {
            XLog.e("onSingleHoleSliderComplete exception:", error?.message);
        }
    }

    //单洞策略修改
    static onSingleHoleStrategySelect(sectionId, strategyId) {
        try{
        //判断本机是否在section段内，如果在，应该要发送指令
        let loops = DataCenter.queryLoopsInSection(sectionId);

        DataCenter.setColonyCabinetDeviceStrategyId(DataCenter.gateId, loops.local, strategyId);
        for(let gate of Object.keys(loops.colony)) {
            DataCenter.setColonyCabinetDeviceStrategyId(gate, loops.colony[gate], strategyId);
        }
        DataCenter.setSegmentCabinetDeviceStrategyId(sectionId, strategyId);
        CommunicationManager.send2Master(formatterSendTopic(Topic_Control), 
        commonMakeControlRequest(
            Control_Level.LEVEL_SEGMENT,
            true,
            sectionId,
            0,
            Adjust_Model.ADJUST_STRATEGY_LIGHT,
            strategyId,[]));
        }catch(error) {
            XLog.e("onSingleHoleStrategySelect exception:", error?.message);
        }
    }

    static onSetLightMode(value){
        try{
            console.log("onSetLightMode: ", value);
            SerialPortManager.setLightMode(value);
            CommunicationManager.send2Remote(formatterSendTopic(Topic_Update_LightMode), 
                commonMakeMqttRequest(ACK_Type.NO_ACK, {lightMode: value}, Topic_Update_LightMode.method));
        }catch(error) {
            XLog.e("onSetLightMode exception:", error?.message);
        }
    }
}